
!
    function(){

        var searchpage=new Rpage.pageObj();

        var cal=new Kalendae($("#dataselect")[0], {
            mode:'multiple'
        });
        $(".dataselect").children("button").click(function(e){
            $("#dataselect").show();
            e.stopPropagation();
        });
        $("#dataselect").click(function(e){
            e.stopPropagation();
        });
        $(document).click(function(e){
            $("#dataselect").hide();
        });


        $(".schedule_type select").change(function(e){
            if($(this).val()==2){
                $(".dataselect").show();
                $(".time_interval").hide();
                $(".data-hour input").val(new Date().getHours());
                $(".data-minute input").val(new Date().getMinutes());
            }else{
                $(".time_interval").show();
                $(".dataselect").hide();
                $(".time_interval_checkbox input")[0].checked=false;
                $(".time_interval_input").hide();
            }
        });
        $(".time_interval_checkbox input").click(function(e){
            if($(this)[0].checked){
                $(".time_interval_input").show();
            }else{
                $(".time_interval_input").hide();
            }
        });
        $(".schedule_type select").change();

        Rswitch.setswitch($(".R_switch"));

        window.setInterval(function(){
            getdata(true);
        },3000);
        function getdata(issavecheckbox){
            if(issavecheckbox){
                var checkarray=[];
                $(".searchtable").children("tbody").children("tr").each(function(index,e){
                    if($(this).children("td:eq(0)").children("input")[0].checked) {
                        checkarray.push(index);
                    }
                });
            }else{
                $.post(SE_LIST,function(data){
                    $(".search_net").html("<label><span class='maincolor musthave'>*</span>采集网站</label>");
                    data= $.parseJSON(data);
                    if(data.code==1) {
                        var datalength=data.result.length;
                        for(var i=0;i<datalength;i++) {
                            $(".search_net").append('<div class="radio-inline">' +
                                '<label>' +
                                '<input type="checkbox" class="secheck" data-name="'+data.result[i].se_name+'"  value="'+data.result[i].id+'">' +data.result[i].se_name+
                                '</label>' +
                                '</div>');
                        }
                    }
                });
                $(".allcheck")[0].checked=false;
            }



            $.post(TASK_LIST,{"type":"2","page":searchpage.pagenum,"pageCount":searchpage.perpage},function(data){
                cleantable($(".searchtable"));
                data= $.parseJSON(data);
                if(data.code==1){
                    var datalength=data.result.length;
                    for(var i=0;i<datalength;i++){
                        var tdstr='<tr data-id="'+data.result[i].id+'"' +
                            ' data-page_count="'+data.result[i].page_count+'" ' +
                            'data-rule-match="'+data.result[i].rule_match+'" ' +
                            'data-schedule_type="'+data.result[i].schedule_type+'" ' +
                            'data-search_id="'+data.result[i].search_id+'" ' +
                            'data-schedule_data="'+(function(data,i){
                                var strt="";
                                if(data.result[i].schedule){
                                    var slength=data.result[i].schedule.length;
                                    for(var j=0;j<slength;j++){
                                        if(j==0){
                                        strt=strt+data.result[i].schedule[j].time;
                                        }else if(j!=0){
                                            strt=strt+","+data.result[i].schedule[j].time;
                                        }
                                    }
                                }
                                return strt;
                            })(data,i)+'">' +
                            '<td><input type="checkbox"></td>' +
                            '<td>'+(function(data){
                                switch (data.result[i].task_status){
                                    case "0":
                                        return "无效";
                                        break;
                                    case "1":
                                        return "运行中";
                                        break;
                                    case "2":
                                        return "停止";
                                        break;
                                    default :
                                        return "未知错误";
                                        break;
                                }
                            })(data)+'</td>' +
                            '<td>'+data.result[i].task_name+'</td>' +
                            '<td>'+data.result[i].search_key+'</td>' +
                            '<td>'+
                            (function(data,i){
                                var showstr="";
                                if(data.result[i].search_id){
                                    var searchid=data.result[i].search_id.split(",");
                                    var searchidlength=searchid.length;
                                    for(var i=0;i<searchidlength;i++){
                                        $(".secheck").each(function(index,e){
                                            if($(this).val()==searchid[i]){
                                                if(showstr==""){
                                                    showstr=showstr+$(this).attr("data-name");
                                                }else{
                                                    showstr=showstr+","+$(this).attr("data-name");
                                                }
                                            }
                                        });
                                    }

                                }
                                return showstr;
                            })(data,i)+'</td>' +
                            '<td>'+(function(data,i){
                                if(data.result[i].schedule_type=="2"){
                                    return "计划执行"
                                }else if(data.result[i].time_interval=="0" || data.result[i].time_interval=="-1"){
                                    return "立即执行";
                                }else{
                                    return data.result[i].time_interval+"秒";
                                }
                            })(data,i)+'</td>' +
                            '<td>'+data.result[i].count_thread_max+'</td>' +
                            '<td>'+(function(data,i){
                                if(data.result[i].schedule_type=="2"){
                                    var timestr="";
                                    if(data.result[i].schedule){
                                        var schedulelength=data.result[i].schedule.length;
                                        for(var j=0;j<schedulelength;j++){
                                            if(j==0){
                                                timestr=timestr+Rcheck.changedate(data.result[i].schedule[j].time);
                                            }else{
                                                timestr=timestr+"<hr>"+Rcheck.changedate(data.result[i].schedule[j].time);
                                            }
                                        }
                                    }else{
                                        timestr="时间未选择";
                                    }

                                    return timestr
                                }else{
                                    return "-";
                                }
                            })(data,i)+'</td>' +
                            '<td class="text-center tablepp"><i class="fa fa-'+(data.result[i].enabled==0?"play":"pause")+'"></i></td>' +
                            '<td><i class="fa fa-edit"></i><i data-toggle="modal" data-target="#myModal"  class="fa fa-trash"></i></td>' +
                            '</tr>';
                        $(".searchtable").children("tbody").append(tdstr);
                    }

                    if(issavecheckbox){
                        $(".searchtable").children("tbody").children("tr").each(function(index,e){
                            var checkarraylength=checkarray.length;
                            for(var i=0;i<checkarraylength;i++){
                                if(checkarray[i]==index){
                                    $(this).children("td:eq(0)").children("input")[0].checked=true;
                                }
                            }
                        });
                    }
                    searchpage.setallpagenum(data.count);
                }
            });
        }
        getdata();
        searchpage.setpagediv($(".table_page"));
        searchpage.setpagechange(function(e){
            getdata();
        });

            $(".searchtable").children("tbody").on("click","tr td i",function(e){
                var trindex=$(this).parent().parent().index();
                switch ($(this).index('.searchtable tbody tr:eq('+trindex+') td i ')){
                case 1:
                    $(".openRwindow").click();
                    $(".R_windows_title").html("修改任务设置");
                    if($(this).parent().parent().children("td:eq(5)").html()!="立即执行"&&$(this).parent().parent().children("td:eq(5)").html()!="计划执行"){
                        $(".time_interval input").val($(this).parent().parent().children("td:eq(5)").html().substring(0,$(this).parent().parent().children("td:eq(5)").html().length-1));
                    }else{
                        $(".time_interval input").val("0")
                    }
                    $(".task_name input").val($(this).parent().parent().children("td:eq(2)").html());
                    $(".search_key input").val($(this).parent().parent().children("td:eq(3)").html());

                    var searchid=$(this).parent().parent().attr("data-search_id");
                    searchid=searchid.split(",");
                    var searchidlength=searchid.length;
                        $(".secheck").each(function(index,e){
                            $(this)[0].checked=false;
                            for(var i=0;i<searchidlength;i++){
                                if($(this).val()==searchid[i]){
                                    $(this)[0].checked=true;
                                    break;
                                }
                            }
                        });

                    $(".page_count input").val($(this).parent().parent().attr("data-page_count"));

                    $(".schedule_type select").val($(this).parent().parent().attr("data-schedule_type"));
                    $(".schedule_type select").change();

                    if($(".time_interval input").val()!="0"){
                        $(".time_interval_checkbox input")[0].checked=true;
                        $(".time_interval_input").show();
                    }

                    var schedule_time=$(this).parent().parent().attr("data-schedule_data");
                    if(schedule_time==""){
                        var slength=0;
                    }else{
                        var schedule_data=schedule_time.split(",");
                        slength=schedule_data.length;

                        $(".data-hour input").val(new Date(schedule_data[0]*1000).getHours());
                        $(".data-minute input").val(new Date(schedule_data[0]*1000).getMinutes());
                        for(var i=0;i<slength;i++){
                            cal.addSelected(new Date(parseInt(schedule_data[i]+"000")));
                        }

                    }




                    $(".count_thread_max input").val($(this).parent().parent().children("td:eq(6)").html());

                    $(".R_switch").attr("data-ison",($(this).parent().parent().children("td:eq(8)").children().hasClass("fa-pause")?"true":"false"));
                    Rswitch.setswitch($(".R_switch"));



                    $(".openRwindow").attr("data-id",$(this).parent().parent().attr("data-id"));
                    break;
                case 0:
                    $.post(UPDATE_TASK,{"id":$(this).parent().parent().attr("data-id"),"enabled":($(this).hasClass("fa-pause")?"0":"1")},function(data){
                        data= $.parseJSON(data);
                        if(data.code==1){
                            getdata();
                        }else{
                            alert(data.result);
                        }
                    });

                    break;
                case 2:
                    $(".delete_yes").attr("data-isall","no");
                    $(".delete_yes").attr("data-id",$(this).parent().parent().attr("data-id"));
                    //$.post(DELETE_TASK,{"id":$(this).parent().parent().attr("data-id")},function(data){
                    //    data= $.parseJSON(data);
                    //    if(data.code==1){
                    //        thistr.remove();
                    //        alert(data.result);
                    //    }else{
                    //        alert(data.result);
                    //    }
                    //});
                    break;
                default :
                    break;
            }
        });
        $(document).ready(function(e){
            $(".delete_yes").click(function(e){
                if($(".delete_yes").attr("data-isall")=="no"){
                    $.post(DELETE_TASK,{"id":$(this).attr("data-id")},function(data){
                        data= $.parseJSON(data);
                        if(data.code==1){
                            getdata();
                            alert(data.result);
                        }else{
                            alert(data.result);
                        }
                    });
                }else{
                    $(".searchtable").children("tbody").children("tr").each(function(index,e){
                        if($(this).children("td:eq(0)").children("input")[0].checked){
                            console.log($(this).attr("data-id"));
                            $.post(DELETE_TASK,{"id":$(this).attr("data-id")},function(data){
                                data= $.parseJSON(data);
                                if(data.code==1){
                                    getdata();
                                }else{
                                    alert(data.result);
                                }
                            });
                        }
                    });
                    $(".allcheck")[0].checked=false;
                }
            });
        });


        $(".searchsetting_ok").click(function(e){
            var postobj={};
            $(".secheck").each(function(index,e){
                if($(this)[0].checked){
                    if(postobj.search_id){
                        postobj.search_id=postobj.search_id+","+$(this).val();
                    }else{
                        postobj.search_id=$(this).val();
                    }
                }
            });
            if(Rcheck.checknull($(".task_name input").val())){
                alert("任务名不得为空");
                return false;
            }else if(Rcheck.checklength($(".task_name input").val(),16)){
                alert("任务名不得超过16个字符");
                return false;
            }else if(Rcheck.checknull($(".search_key input").val())){
                alert("关键词不得为空");
                return false;
            }else if(Rcheck.checklength($(".search_key input").val(),50)){
                alert("关键词不得超过50个字符");
                return false;
            }else if(!postobj.search_id){
                alert("采集网站不得为空");
                return false;
            }else if(Rcheck.checknull($(".count_thread_max input").val())){
                alert("爬虫数量不得为空");
                return false;
            }else if($(".count_thread_max input").val()>10){
                alert("爬虫数量不得大于10");
                return false;
            }else if(Rcheck.checknull($(".page_count input").val())){
                alert("每次采集条数不得为空");
                return false;
            }else if(Rcheck.checknum($(".page_count input").val())){
                alert("每次采集页面数必须为数字");
                return false;
            }else if(Rcheck.checknum($(".count_thread_max input").val())){
                alert("爬虫数量必须为数字");
                return false;
            }
            if(($(".schedule_type select").val()==1&&$(".time_interval_checkbox input")[0].checked)){
                if(Rcheck.checknull($(".time_interval_input_val input").val())){
                    alert("重复执行间隔不得为空");
                    return false;
                }
                if(Rcheck.checknum($(".time_interval_input_val input").val())){
                    alert("重复执行间隔必须为数字");
                    return false;
                }
                if($(".time_interval_input_val input").val()>100000000000000000000000){
                    alert("重复执行间隔数字过大");
                    return false;
                }
            }

            postobj.page_count=$(".page_count input").val();
            postobj.task_name=$(".task_name input").val();
            postobj.task_type="2";
            postobj.count_thread_max=$(".count_thread_max input").val();
            postobj.time_interval=(($(".schedule_type select").val()==1&&$(".time_interval_checkbox input")[0].checked)?$(".time_interval_input_val input").val():0);
            postobj.search_key=$(".search_key input").val();
            postobj.schedule_type=$(".schedule_type select").val();
            if(postobj.schedule_type==2){
                postobj.schedule_data=[];
                var callength=cal._sel.length;
                for(var i=0;i<callength;i++){
                    var calhour=$(".data-hour input").val()*3600;
                    var calminute=$(".data-minute input").val()*60;
                    postobj.schedule_data.push(cal._sel[i]._d.getTime()/1000-(-calhour)-(-calminute));
                }
            }


            postobj.enabled=($(".R_switch").attr("data-ison")=="true"?1:0);
            if($(".R_windows_title").html()=="添加任务"){
                $.post(ADD_TASK,postobj,function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        getdata();
                        alert("添加成功");
                        $(".R_windows").hide();
                    }else{
                        alert(data.result);
                    }

                });
            }else{
                postobj.id=$(".openRwindow").attr("data-id");
                $.post(UPDATE_TASK,postobj,function(data){
                    data= $.parseJSON(data);
                    if(data.code==1){
                        getdata();
                        alert("修改成功");
                        $(".R_windows").hide();
                    }else{
                        alert(data.result);
                    }
                });
            }
        });


        $(".allcheck").click(function(e){
            $(".searchtable").children("tbody").children("tr").each(function(index,e){
                $(this).children("td:eq(0)").children("input")[0].checked= $(".allcheck")[0].checked;
            });
        });


        $(".alldelete").click(function(e){
            $(".delete_yes").attr("data-isall","yes");
        });



        $(".allstart").click(function(e){

            $(".searchtable").children("tbody").children("tr").each(function(index,e){
                if($(this).children("td:eq(0)").children("input")[0].checked){
                    var thistr=$(this);
                    $.post(UPDATE_TASK,{"id":$(this).attr("data-id"),"enabled":"1"},function(data){
                        data= $.parseJSON(data);
                        if(data.code==1){
                            getdata();
                        }else{
                            alert(data.result);
                        }
                    });
                }
            });
            $(".allcheck")[0].checked=false;
        });

        $(".allstop").click(function(e){
            $(".searchtable").children("tbody").children("tr").each(function(index,e){
                if($(this).children("td:eq(0)").children("input")[0].checked){
                    var thistr=$(this);
                    $.post(UPDATE_TASK,{"id":$(this).attr("data-id"),"enabled":"0"},function(data){
                        data= $.parseJSON(data);
                        if(data.code==1){
                            getdata();
                        }else{
                            alert(data.result);
                        }
                    });
                }
            });
            $(".allcheck")[0].checked=false;
        });

    }();